package Servlet;

import bean.User;
import dao.UserDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;


@WebServlet(name = "chat_Register", value = "/chat_Register")
public class chatRegister extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String userName = request.getParameter("userName");
        String passWord = request.getParameter("passWord");


        //创建user对象承接数据
        User user = new User();
        user.setUserName(userName);
        user.setPassWord(passWord);

        UserDao userDao = new UserDao();
        boolean flag = false;
        try {
            flag = userDao.insert(user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (flag) {
            request.setAttribute("iisCreate", "istrue");
            request.getRequestDispatcher("/index.jsp").forward(request, response);
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        } else {
            request.setAttribute("isRegister", "isfalse");
            request.getRequestDispatcher("/register.jsp").forward(request, response);
            PrintWriter out = response.getWriter();

        }
    }

        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request,response);
        }
}
